<template>
	<view style="color: #fff;width: 100%;height: 500rpx;line-height: 500rpx;text-align: center;" v-if="platform=='devtools'">
		请下载开心小圈APP进行认证
	</view>
	<view class="content" v-else>
		<view class="stepbox">
			<!-- <u-steps :list="numList" active-color="#fff" :current="stepidx"></u-steps> -->
			<view style="display: flex;align-items: center;font-size: 30rpx;width: 90%;margin: auto;justify-content: center;">
				<view style="color: #D6A666;margin-right: 38rpx;">上传照片</view>
				<u-icon name="arrow-right" size="30" :color="stepidx < 1?'#fff':'#D6A666'"></u-icon>
				<view style="margin: 0 38rpx;" :style="stepidx < 1?'color: #fff;':'color: #D6A666;'">自拍照片</view>
				<u-icon name="arrow-right" size="30" :color="stepidx < 2?'#fff':'#D6A666'"></u-icon>
				<view style="margin-left: 38rpx;" :style="stepidx < 2?'color: #fff;':'color: #D6A666;'">完成认证</view>
			</view>
		</view>
		<view class="step" v-if="stepidx == 0">
			<view style="height: 40rpx;"></view>
			<view class="pic">
				<view class="chooseimage flex-col flex-c" @click="select(1)">
					<image mode="aspectFill" v-if="img1.path" :src="img1.path"></image>
					<block v-else>
						<u-icon name="plus" color="#777777" size="64"></u-icon>
					</block>
				</view>
			</view>
			<view style="height: 100rpx;"></view>
			<view style="text-align: center;width: 100%;font-size: 25rpx;">
				<view style="color: #fff;">请上传五官清晰的正脸照片，避免过度美颜</view>
				<view style="color: #D6A666;margin-top: 20rpx;">请勿上传暴露照片，认证成功后将上传到相册内</view>
			</view>
			<view style="width: 100%;text-align: center;margin-top: 50rpx;">
				<button class="vie-bott" @click="next">下一步</button>
			</view>
			<view style="height: 330rpx;"></view>
			<view style="position: fixed;bottom: 20rpx;width: 100%;">
				<view style="width: 80%;height: 80rpx;font-size: 30rpx;color: #fff;display: flex;align-items: center;margin: auto;justify-content: space-between;">
					<view>——————</view>
					<view>认证成功可获得奖励</view>
					<view>——————</view>
				</view>
				<view style="height: 50rpx;"></view>
				<view style="display: flex;align-items: center;font-size: 30rpx;color: #fff;height: 200rpx;">
					<view style="text-align: center;width: 33.3%;">
						<image src="../../static/renzheng/renzheng.png" style="width: 102rpx;height: 102rpx;"></image>
						<view style="margin-top: 40rpx;">真人认证标识</view>
					</view>
					<view style="text-align: center;width: 33.3%;">
						<image src="../../static/renzheng/huiyuan.png" style="width: 102rpx;height: 102rpx;"></image>
						<view style="margin-top: 40rpx;">真人认证标识</view>
					</view>
					<view style="text-align: center;width: 33.3%;">
						<image src="../../static/renzheng/tuijian.png" style="width: 102rpx;height: 102rpx;"></image>
						<view style="margin-top: 40rpx;">真人认证标识</view>
					</view>
				</view>
			</view>
		</view>
		<view class="step" v-else-if="stepidx == 1">
			<view class="face flex-col flex-c">
				<image src="../../static/renzheng/shibie.png" mode="widthFix"></image>
				<view style="height: 180rpx;"></view>
				<view style="text-align: center;width: 100%;font-size: 25rpx;">
					<view style="color: #fff;">自拍照后，自动进行相册对比</view>
					<view style="color: #D6A666;margin-top: 20rpx;">保护用户隐私，自拍照片不会公开</view>
				</view>
			</view>
			<view style="width: 100%;text-align: center;margin-top: 40rpx;">
				<button class="vie-bott" @click="paizhao">拍照识别</button>
			</view>
		</view>
		<view class="step" v-else-if="stepidx == 2">
			<view class="face flex-col flex-c">
				<image src="/static/img_face_certification.png" mode="widthFix"></image>
				<view>认证成功</view>
			</view>
			<view style="width: 100%;text-align: center;margin-top: 40rpx;">
				<button class="vie-bott" @click="back">完成</button>
			</view>
		</view>
	</view>
</template>

<script>
	import OCR from '@/common/BaiDu-OCR/BaiDu-OCR.js';
	import {
		pathToBase64,   
		base64ToPath
	} from 'image-tools';
	export default {
		data() {
			return {
				stepidx: 0,
				action: '',
				numList: [{
					name: '上传照片'
				}, {
					name: '面容识别'
				}, {
					name: '认证完成'
				}],
				img1: {
					path: '',
					base64: ''
				},
				img2: {
					path: '',
					base64: ''
				},
				platform:''
			}
		},
		onLoad() {
			// #ifdef MP-WEIXIN
					this.platform = 'devtools' // 14.3
			// #endif
		},
		methods: {
			next(){
				// #ifdef MP-WEIXIN
					return this.$u.toast('请下载开心小圈APP进行认证')
				// #endif
				if(this.img1.path && this.img1.base64){
					this.stepidx++
				}else{
					this.$u.toast('请上传图片')
				}
			},
			back(){
				uni.navigateBack()
			},
			select(e){
				var _this = this
				uni.chooseImage({
					count: 1,
					success(res){
						_this.img1.path = res.tempFilePaths[0]
						// console.log(res.tempFilePaths[0]).then(result=>{
						// 	_this.img1.base64 = result
						// });
						_this.p2B64(res.tempFilePaths[0]).then(result=>{
							_this.img1.base64 = result.split(",")[1]
						})
						// _this.p2B64(res.tempFilePaths[0],1)
					}
				})
			},
			paizhao(){
				// 人脸一致
				// this.faceauth()
				var _this = this
				uni.chooseImage({
					sourceType: ['camera'],
					count: 1,
					success(res){
						_this.p2B64(res.tempFilePaths[0]).then(async picresult=>{
							_this.img2.base64 = picresult.split(",")[1];
							console.log(_this.img2.base64);
							uni.showLoading({
								mask:true
							})
							console.log(_this.img2.base64)
							// 先进行活体识别
							var {result} = await OCR.ocr({func:'faceverify',image:_this.img2.base64})
							console.log(result)
							if(result && result.face_liveness>0.3){
								if(result.face_list[0].face_probability>0.9){
									// 再对比第一张图人脸是否一致
									setTimeout(()=>{
										_this.duibi(_this.img2.base64)
									}, 2200)
								}else{
									_this.$u.toast('请重试')
								}
							}else{
								_this.$u.toast('人脸活体检测失败')
							}
						})
							// _this.p2B64(res.tempFilePaths[0],2)
							
					}
				})
			},
			async duibi(val){
				console.log(this.img1.base64);
				console.log(val);
				var {result} = await OCR.ocr({func:'match',Live_Image:this.img1.base64,IDCard_Image:val})
				console.log(result)
				if(result&&result.score>=80){
					// 人脸一致
					this.faceauth()
				}else{
					this.$u.toast('人脸不一致')
				}
			},
			faceauth(){
				var _this = this
				this.$Upimg({
					image: _this.img1.path
				}, async (result)=>{
					const {data:res} = await _this.$Request({
						url: 'api/Faceid/authover',
						method: 'POST',
						data: {
							user_id: _this.$store.state.userid,
							image: result
						}
					})
					if (res.code == 200) {
						_this.stepidx = 2
						uni.hideLoading()
					} else {
						_this.$u.toast('验证失败')
					}
				})
			},

			//本地图片路径转换base64
			p2B64(path,i) {
				var that = this
				console.log('path:', path)
				
				// uni.getFileSystemManager().readFile({
				// 	filePath: path,
				// 	encoding: 'base64',
				// 	success: res => {
				// 		console.log(res.data)
				// 		that.img1.base64 = 'data:image/png;base64,'+res.data
				// 	},
				// 	fail: (e) => {
				// 		console.log(e)
				// 	}
				// })
				
				
				
				
				return new Promise(function(resolve, reject) {
					pathToBase64(path).then(data => {
							resolve(data)
						// if(i == 1){
						// 	that.img1.base64 = data;
						// }else{
						// 	that.img2.base64 = data;
						// }
										
					})
					// plus.io.resolveLocalFileSystemURL(path, function(entry) {
					// 	console.log(path)
					//     entry.file(function(file) {
					//         var fileReader = new plus.io.FileReader()
					//         fileReader.onload = function(evt) {
					//             resolve(evt.target.result)
					//         }
					//         fileReader.onerror = function(error) {
					//             reject(error)
					//         }
					//         fileReader.readAsDataURL(file)
					//     }, function(error) {
					//         reject(error)
					//     })
					// }, function(error) {
					//     reject(error)
					// })
					// uni.getFileSystemManager().readFile({
					// 	filePath: path,
					// 	encoding: 'base64',
					// 	success: function(res) {
					// 		resolve(res.data)
					// 	},
					// 	fail: function(error) {
					// 		reject(error)
					// 	}
					// })
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		/* #ifndef H5 */
		min-height: 100vh;
		/* #endif */
		/* #ifdef H5 */
		min-height: calc(100vh - 44px);
		/* #endif */
		background-color: #000;
		overflow: hidden;
		.stepbox{
			padding: 60rpx 30rpx;
		}
		.label{
			color: #eee;
			padding: 20rpx 30rpx;
		}
		.li{
			color: #999;
			padding: 4rpx 30rpx;
			font-size: 24rpx;
		}
		.pic{
			width: 100%;
			display: flex;
			justify-content: center;
		}
		.face{
			color: #fff;
			font-size: 32rpx;
			padding: 60rpx 30rpx;
			image{
				width: 40vw;
			}
			text{
				margin-top: 20rpx;
				color: #999;
				font-size: 24rpx;
				text-align: center;
			}
		}
		.chooseimage{
			width: 301rpx;
			height: 301rpx;
			border-radius: 10rpx;
			background-color: #323232;
			image{
				width: 301rpx;
				height: 301rpx;
				border-radius: 10rpx;
			}
			view{
				margin-top: 20rpx;
			}
		}
	}
	.vie-bott{
		background-color: #D89E5E;
		width: 443rpx;
		height: 84rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
		font-size: 35rpx;
		border-radius: 40rpx;
	}
</style>
